package recycle.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import recycle.pojo.Admin;
import recycle.service.AdminService;
import recycle.utils.Page;
import recycle.vo.AdminParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 描述：后台管理员信息表 控制器
* @author 孙冉
* @date 2019/07/22
*/
@RestController
@RequestMapping("/admin")
@Api(value = "有关Admin的操作",description = "有关Admin的所有操作")
public class AdminController{
	@Resource
    private AdminService adminService;
 	
 	@RequestMapping("/findById.json")
    public Admin findById(Integer adminId){
        Admin result = null;
        try {
            result = adminService.findById(adminId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 描述：获取所有的Admin对象
     */
    @ApiOperation(value = "获取所有Admin对象",response = Admin.class)
    @RequestMapping("/findAll.json")
    public List<Admin> findAll(){
        List<Admin> result = null;
        try {
            result = adminService.findAll();
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 描述：新增保存Admin对象
     */
    @RequestMapping("/save.json")
    public Map save(Admin admin){
        Map result = new HashMap();
        result.put("flag",false);
        result.put("msg","保存后台管理员信息表失败");
        try {
            if(adminService.save(admin)>0){
                result.put("flag",true);
                result.put("msg","保存后台管理员信息表成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 描述：根据主键删除后台管理员信息表对象
     * @param adminId
     */
    @RequestMapping("/delete.json")
    public Map delete(Integer adminId){
        Map result = new HashMap();
        result.put("flag",false);
        result.put("msg","保存后台管理员信息表失败");
        try {
            if(adminService.delete(adminId)>0){
                result.put("flag",true);
                result.put("msg","保存后台管理员信息表成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 描述：更新后台管理员信息表对象
     */
    @RequestMapping("/update.json")
    public Map update(Admin admin){
        Map result = new HashMap();
        result.put("flag",false);
        result.put("msg","更新后台管理员信息表失败");
        try {
            if( adminService.update(admin)>0){
                result.put("flag",true);
                result.put("msg","更新后台管理员信息表成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;

    }

    /**
     * 根据不确定的条件，动态查询
     */
    @RequestMapping("/query.json")
    public List<Admin> query(AdminParam param){
        List<Admin> result = null;
        try {
            result = adminService.query(param);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 根据不确定的条件，动态分页查询
     */
    @RequestMapping("/queryByPage.json")
    public Page queryByPage(AdminParam param){
        Page result = null;
        try {
            result = adminService.queryByPage(param);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }
    @RequestMapping("/findByadminId.json")
    public List<Admin> findByadminId(int adminId){
        return adminService.findByadminId(adminId);
    }

    @RequestMapping("/findByResponsible.json")
    public List<Admin> findByResponsible(String  responsible){
        return adminService.findByResponsible(responsible);
    }


    @RequestMapping("/findByType.json")
    public List<Admin> findByType(String  type){
        return adminService.findByResponsible(type);
    }


    @RequestMapping("/findByNameAndPassword.json")
    public int findByNameAndPassword(String other1,String password,int type){
         Admin admin=adminService.findByOther1(other1);
         if(admin!=null) {
             if ( password.equals(admin.getPassword()) && type == 4) {
                 return 3;
             } else if ( password.equals(admin.getPassword()) && type == 3) {
                 return 2;
             } else if ( password.equals(admin.getPassword()) && type == 2) {
                 return 1;
             }
         }
            return 0;
    }
@RequestMapping("/findAllAdmin.json")
   public List<Admin> findAllAdmin(){


        return adminService.findAllAdmin();
   }
    @RequestMapping("/saveNew.json")
   public int saveNew(Admin admin){
        Date date =new Date();
        admin.setModifiedTime(date);
        return adminService.saveNew(admin);
   }
    @RequestMapping("/findByNameAndType.json")
    public int findByNameAndType(String other1,int type){
        Admin admin=adminService.findByOther1(other1);
        if(admin!=null) {
            if (type == 3) {
                return 3;
            } else if ( type == 2) {
                return 2;
            } else if ( type == 1) {
                return 1;
            }
        }
        return 0;
    }
 }
